var fbAppId = "218519764993619";
	var permissions_needed = "email,user_location,user_hometown,user_relationship_details,user_relationships,user_birthday";
    (function(d){
      var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
      js = d.createElement('script'); js.id = id; js.async = true;
      js.src = "//connect.facebook.net/en_US/all.js";
      d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
    window.fbAsyncInit = function() {
      FB.init({
        appId      : fbAppId,
        status     : true,
        cookie     : true,
        xfbml      : true 
      });
        FB.Event.subscribe('auth.statusChange', function(response) {
			if (response.authResponse) {
				FB.api('/me', function(user){
					if (user.name) {				
						$.ajax({
						            type: "POST",
						            url: "/users/social",
						            data: user,
						            success: function(data){
					                      console.log(data);
								var data = JSON.parse(data);
								if(data.status == 'success'){
								   	
									if(data.redirect == 1){
									
										
									    	 alert(data.msg);
										
										window.location = "/users/index";
									}
								} else if( data.status == 'error') {			
									  if(data.redirect == 0){
									  	FB.logout();
									  	alert(data.msg);
									  }
								} else {			
									  alert('Error');
								}
								

							     }
					     });
					}
				})
				document.getElementById('auth-loggedout').style.display = 'none';
				document.getElementById('auth-loggedin').style.display = 'block';
			} else {
				document.getElementById('auth-loggedout').style.display = 'block';
				document.getElementById('auth-loggedin').style.display = 'none';
			}
        });
    };

	function fbLogin() {
		FB.login(function (response) {}, {scope: permissions_needed});
	}

	function fbLogout() {
		FB.logout();
		$.ajax({
						            type: "POST",
						            url: "/users/logout",
						            success: function(data){
					                      console.log(data);
								var data = JSON.parse(data);
								   if(data.status == 'success'){
										window.location = "/users/login";
										}
								
							 }   
					     }).fail(function(){
					     	window.location = "/users/login";
					     });
	}